package com.cloudcreativity.blyg.utils;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.databinding.ObservableField;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;

import com.cloudcreativity.blyg.R;
import com.cloudcreativity.blyg.databinding.LayoutDialogAmountBinding;

public class AmountDialog extends Dialog {

    public ObservableField<String> name = new ObservableField<>();
    public ObservableField<String> unit = new ObservableField<>();
    public ObservableField<String> specName = new ObservableField<>();

    public ObservableField<String> amount = new ObservableField<>();

    private OnOkListener onOkListener;

    public void setOnOkListener(OnOkListener onOkListener) {
        this.onOkListener = onOkListener;
    }

    public AmountDialog(@NonNull Context context, String nameStr, String unitStr, String specNameStr) {
        super(context, R.style.myProgressDialogStyle);
        name.set(nameStr);
        unit.set(unitStr);
        specName.set(specNameStr);
        setCanceledOnTouchOutside(true);
        LayoutDialogAmountBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context),
                R.layout.layout_dialog_amount,
                null,
                false);
        setContentView(binding.getRoot());
        binding.setUtils(this);
    }

    public void onOk(){
        if(onOkListener!=null){
            if(TextUtils.isEmpty(amount.get())){
                return;
            }
            float parseFloat = Float.parseFloat(amount.get());
            onOkListener.onOk(parseFloat);
        }
        KeyBoardHelper.close(getContext(),this);
        dismiss();
    }

    public interface OnOkListener{
        void onOk(float amount);
    }
}
